class Solution
{
public:
    int flipChess(vector<string> &chessboard)
    {
        int m = chessboard.size();
        int n = chessboard[0].size();

        int maxFlip = 0;
        auto bfs = [&chessboard, m, n](int startRow, int startCol) -> int
        {
            auto chessboardCopy = chessboard;
            chessboardCopy[startRow][startCol] = 'X';
            queue<pair<int, int>> q;
            q.push({startRow, startCol});
            int count = 0;
            while (!q.empty())
            {
                int qSize = q.size();
                for (int _ = 0; _ < qSize; ++_)
                {
                    auto pos = q.front(); // 这里用结构化绑定无法被内层lamdba捕获
                    int row = pos.first;
                    int col = pos.second;
                    q.pop();
                    auto tryFlip = [row, col, m, n, &chessboardCopy, &q](int dx, int dy) -> int
                    {
                        int step = 0;
                        int nextRow = row;
                        int nextCol = col;
                        int flipCount = 0;
                        while (true)
                        {
                            ++step;
                            nextRow += dx;
                            nextCol += dy;
                            if (nextRow < 0 || nextCol < 0 || nextRow >= m || nextCol >= n)
                            {
                                return 0;
                            }
                            if (chessboardCopy[nextRow][nextCol] == '.')
                            {
                                return 0;
                            }
                            if (chessboardCopy[nextRow][nextCol] == 'X')
                            {
                                break;
                            }
                        }
                        nextRow = row;
                        nextCol = col;
                        for (int _ = 1; _ < step; ++_)
                        {
                            nextRow += dx;
                            nextCol += dy;
                            chessboardCopy[nextRow][nextCol] = 'X';
                            q.push({nextRow, nextCol});
                            ++flipCount;
                        }
                        return flipCount;
                    };
                    count += tryFlip(0, 1);
                    count += tryFlip(1, 0);
                    count += tryFlip(0, -1);
                    count += tryFlip(-1, 0);
                    count += tryFlip(1, 1);
                    count += tryFlip(-1, 1);
                    count += tryFlip(1, -1);
                    count += tryFlip(-1, -1);
                }
            }
            return count;
        };

        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (chessboard[i][j] != '.')
                {
                    continue;
                }
                maxFlip = max(maxFlip, bfs(i, j));
            }
        }

        return maxFlip;
    }
};